package server;

import java.net.ServerSocket;
import java.net.Socket;

/**
 * A simple TCP server application. The application waits for incoming
 * connections on a specified port. For each incoming connection it reads an
 * item of text, and echoes back that received text in upper case.
 * 
 * @author Eduardo Marques
 */
public class PISTServer {
    /**
     * Entry point for the program.
     * 
     * @param args
     *            Program arguments
     * @throws Exception
     *             if an error occurs.
     */
    public static void main(String[] args) throws Exception {
        // Port variable.
        int port = Integer.parseInt(args[0]);

        // Create server socket.
        @SuppressWarnings("resource")
        ServerSocket serverSocket = new ServerSocket(port);
        //serverSocket.setSoTimeout(3000);

        // Server loop.
        while (true) {
            // Wait for a client connection.
            Socket connectionSocket = serverSocket.accept();
            Thread ts = new Thread(new ThreadedPISTServer(connectionSocket));
            ts.start();
            // serverSocket.close();
        }
    }
}
